﻿

#include "zyButton.h"

BlackMoonUI::zyButton::zyButton()
{
	SetMember(L"this", (LONG_PTR)this);
	m_obj->setWndProc(zyButton_WndProc);
	m_obj->setDestroy(zyButton_Destroy);
	setType(zyType_Button);
}
BlackMoonUI::zyButton::~zyButton()
{

}
void BlackMoonUI::zyButton::OnNCDestroy()
{
	HICON hIcon = (HICON)GetMember(L"BtnIcon");
	if (hIcon != 0) {
		DestroyIcon(hIcon);
		SetMember(L"BtnIcon", 0);
	}
}
void WINAPI BlackMoonUI::zyButton::zyButton_Destroy(zyObject* obj)
{
	zyButton* zyb = (zyButton*)obj->getPtrData(L"this");
	zyb->OnNCDestroy();
	zyCommandCtrol::zyCommand_Destroy(obj);
}
LRESULT WINAPI BlackMoonUI::zyButton::zyButton_WndProc(zyObject* obj, HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL* bHandled)
{

	if (uMsg == WM_NCDESTROY) {
		zyButton* zyb = (zyButton*)obj->getPtrData(L"this");
		zyb->OnNCDestroy();
	}
	else if (uMsg == WM_KEYDOWN) {
		if (wParam == 13)//13回车键
			SendMessage(hWnd, BM_CLICK, 0, 0);
	}
	return zyCommandCtrol::zyCommand_WndProc(obj, hWnd, uMsg, wParam, lParam, bHandled);
}

BOOL BlackMoonUI::zyButton::创建(HWND 参数_父窗口, const std::wstring& 参数_标题, POINT 参数_位置, POINT 参数_尺寸, int 参数_ID, int 参数_窗口风格, int 参数_扩展风格)
{
	if (参数_窗口风格 == 0)
		参数_窗口风格 = WS_TABSTOP;
	if (参数_ID == NULL)
		参数_ID = 黑月默认ID();

	return _CreateCtrol(参数_父窗口, 参数_标题, 参数_位置, 参数_尺寸, 参数_ID, 参数_窗口风格, 参数_扩展风格, L"Button");
}

BOOL BlackMoonUI::zyButton::创建(zyWindow& 参数_父窗口, const std::wstring& 参数_标题, POINT 参数_位置, POINT 参数_尺寸, int 参数_ID, int 参数_窗口风格, int 参数_扩展风格)
{
	if (参数_窗口风格 == 0)
		参数_窗口风格 = WS_TABSTOP;
	if (参数_ID == NULL)
		参数_ID = 黑月默认ID();

	return _CreateCtrol(参数_父窗口.getHwnd(), 参数_标题, 参数_位置, 参数_尺寸, 参数_ID, 参数_窗口风格, 参数_扩展风格, L"Button");

}

void BlackMoonUI::zyButton::事件_被单击(TRegEvent* 参数_事件指针)
{
	SetRegEvent(L"OnClick", 参数_事件指针);
}
BlackMoonUI::zyIcon BlackMoonUI::zyButton::取图标() {
	HICON hIcon = (HICON)sendMsg(BM_GETIMAGE, IMAGE_ICON, 0);
	zyIcon 局部_图标;
	局部_图标.setIcon(CopyIcon(hIcon));
	return 局部_图标;
}
void BlackMoonUI::zyButton::置图标(zyIcon& 参数_图标) {
	HICON hIcon = (HICON)GetMember(L"BtnIcon");
	if (hIcon != 0)
		DestroyIcon(hIcon);
	AddStyle(BS_ICON);
	hIcon = CopyIcon(参数_图标.getIcon());
	SetMember(L"BtnIcon", (LONG_PTR)hIcon);
	sendMsg(BM_GETIMAGE, IMAGE_ICON, (LPARAM)hIcon);
}
bool BlackMoonUI::zyButton::取消按钮(bool 参数_取消按钮) {//参数留空为取，否则为置。获取或设置按钮是否取消按钮
	HWND hWndTop = GetAncestor(取窗口句柄 (), GA_ROOT);
	zyObject* tempThis = zyEnv::getInstance().getWndObject(hWndTop);
	if (tempThis != nullptr) {
		zyObject* CancelButton = (zyObject*)tempThis->getPtrData(L"CancelButton");
		if (CancelButton != nullptr) {
			CancelButton->release();
			//IUnknown_Release (CancelButton);
		}
		if (参数_取消按钮) {
			CancelButton = getObject ();
			CancelButton->addRef();
			//IUnknown_AddRef (CancelButton);
			tempThis->setPtrData(L"CancelButton", CancelButton);
		}
		else
			tempThis->setPtrData(L"CancelButton", 0);
	}
	return 参数_取消按钮;
}
bool BlackMoonUI::zyButton::取消按钮() {//参数留空为取，否则为置。获取或设置按钮是否取消按钮
	DWORD Default = GetStyle() & BS_DEFPUSHBUTTON;
	return Default != 0;
}
std::wstring BlackMoonUI::zyButton::标题(const std::wstring& 参数_标题) {
	if (参数_标题.empty()) {
		return 取文本();
	}
	else {
		置文本(参数_标题);
        return L"";
	}
}
bool BlackMoonUI::zyButton::默认按钮(bool 参数_默认按钮) {
	HWND hWndTop = 黑月取根窗口(取窗口句柄());
	zyObject* tempThis = zyEnv::getInstance().getWndObject(hWndTop);
	if (tempThis != nullptr) {
		zyObject* DefaultButton = (zyObject*)tempThis->getPtrData(L"DefaultButton");
		if (DefaultButton != nullptr) {
			DefaultButton->release();
			//IUnknown_Release (CancelButton);
		}
		if (参数_默认按钮) {
			AddStyle(BS_DEFPUSHBUTTON);
			DefaultButton = getObject();
			DefaultButton->addRef();
			//IUnknown_AddRef (CancelButton);
			tempThis->setPtrData(L"DefaultButton", DefaultButton);
		}
		else {
			RemoveStyle(BS_DEFPUSHBUTTON);
			tempThis->setPtrData(L"DefaultButton", 0);
		}
	}
	return 参数_默认按钮;
}
bool BlackMoonUI::zyButton::默认按钮() {//参数留空为取，否则为置。获取或设置按钮是否取消按钮
	DWORD Default = GetStyle() & BS_DEFPUSHBUTTON;
	return Default != 0;
}